﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.WinForms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;

namespace Tunnel_Cross_Section.DeviceConfig
{
    public partial class DeviceConfigFrm : Office2007Form
    {

        private DataBase md;
        private DeviceConfigHelper helper;
        public DeviceConfigFrm(DataBase m)
        {
            InitializeComponent();
            this.md = m;
            helper = new DeviceConfigHelper(md);
            this.advTree1.AfterCellEditComplete += new CellEditEventHandler(advTree1_AfterCellEditComplete);
            this.advTree1.AfterNodeSelect += new AdvTreeNodeEventHandler(advTree1_AfterNodeSelect);
        }

        void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            if (e.Node.Level > 0)
            {
                doubleInput1.Value = helper.OnDeviceSelected(e.Node.Text);
            }
        }

        private void DeviceConfigFrm_Load(object sender, EventArgs e)
        {
            LoadNodes();
        }
        private void advTree1_AfterCellEditComplete(object sender, CellEditEventArgs e)
        {
            string oldV = e.Cell.Parent.Tag.ToString();
            string newV = e.NewText;
            helper.UpdateDeviceNo(oldV, newV);
        }
        private void LoadNodes()
        {
            Pnode.Nodes.Clear();
            foreach(string s in helper.GetDeviceNames())
            {
                Node n = new Node() { Text = s, Tag = s };
                n.NodeDoubleClick += delegate
                {
                    n.BeginEdit();
                };
                Pnode.Nodes.Add(n);
            }
        }

        private void doubleInput1_ValueChanged(object sender, EventArgs e)
        {
            double v = doubleInput1.Value;
            helper.UpdateDeviceValue(advTree1.SelectedNode.Text, v);
        }

        private void Pnode_NodeDoubleClick(object sender, EventArgs e)
        {
            RegisterDeviceFrm frm = new RegisterDeviceFrm(this.helper);
            frm.Save += new EventHandler(frm_Save);
            frm.ShowDialog();
           
        }

        void frm_Save(object sender, EventArgs e)
        {
            RegisterDeviceFrm s = sender as RegisterDeviceFrm;
            s.Close();
            LoadNodes();
          
        }

    }
}
